package org.tourism.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.tourism.VO.UserVO;
import org.tourism.entity.User;
import org.tourism.mapper.UserMapper;
import org.tourism.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public UserVO getUserByNameAndPassword(String name, String password) {
        UserVO userVO = new UserVO();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", name);
        queryWrapper.eq("password", password);
        List<User> users = this.list(queryWrapper);
        if (users != null && !users.isEmpty()) {
            User user = users.getFirst();
            userVO.setUsername(name);
            userVO.setPassword(password);
            userVO.setAge(user.getAge());
            userVO.setGender(user.getGender());
            userVO.setUid(user.getUid());
            userVO.setOrigin(user.getOrigin());
            userVO.setPhone(user.getPhone());
            userVO.setIdentity(user.getIdentity());
            return userVO;
        }else{
            return null;
        }
    }

    @Override
    public UserVO getUserByNamePasswordIdentity(String username, String password, String identity) {
        UserVO userVO = new UserVO();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", password);
        queryWrapper.eq("identity", identity);
        List<User> users = this.list(queryWrapper);
        if (users != null && !users.isEmpty()) {
            User user = users.getFirst();
            userVO.setUsername(username);
            userVO.setPassword(password);
            userVO.setAge(user.getAge());
            userVO.setGender(user.getGender());
            userVO.setUid(user.getUid());
            userVO.setOrigin(user.getOrigin());
            userVO.setPhone(user.getPhone());
            userVO.setIdentity(user.getIdentity());
            return userVO;
        }else{
            return null;
        }
    }

    @Override
    public void addUser(UserVO userVO) {
        User user = new User();
        user.setUsername(userVO.getUsername());
        user.setPassword(userVO.getPassword());
        user.setAge(userVO.getAge());
        user.setGender(userVO.getGender());
        user.setUid(userVO.getUid());
        user.setOrigin(userVO.getOrigin());
        user.setPhone(userVO.getPhone());
        user.setIdentity(userVO.getIdentity());
        userMapper.insert(user);
    }

    @Override
    public UserVO getUserById(int uid) {
        UserVO userVO = new UserVO();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        List<User> users = this.list(queryWrapper);
        if (users != null && !users.isEmpty()) {
            User user = users.getFirst();
            userVO.setUsername(user.getUsername());
            userVO.setPassword(user.getPassword());
            userVO.setAge(user.getAge());
            userVO.setGender(user.getGender());
            userVO.setUid(user.getUid());
            userVO.setOrigin(user.getOrigin());
            userVO.setPhone(user.getPhone());
            userVO.setIdentity(user.getIdentity());
            return userVO;
        }else{
            return null;
        }
    }
}
